<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">

      <el-form-item label="转账单号" prop="requestId">
        <el-input v-model="queryParams.requestId" placeholder="请输入转账单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="收款人姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入收款人姓名	" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="银行卡号" prop="bankCardNum">
        <el-input v-model="queryParams.bankCardNum" placeholder="请输入银行卡号	" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="店铺名称" prop="shopName">
        <el-input v-model="queryParams.shopName" placeholder="请输入店铺名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态 " prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态 " clearable>
          <el-option label="转账中" :value="1" />
          <el-option label="转账成功" :value="2" />
          <el-option label="转账失败" :value="3" />
          <el-option label="转账取消" :value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="到账时间" prop="paymentDate">
        <el-date-picker clearable v-model="dateRange" type="daterange" range-separator="至" value-format="yyyy-MM-dd"
          :picker-options="pickerOptions" start-placeholder="开始日期" end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['ecs:externalTransferRecord:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="externalTransferRecordList" @selection-change="handleSelectionChange">
      <el-table-column label="转账单号" align="center" prop="requestId" width="280" />
      <el-table-column label="店铺名称" align="center" prop="shopName" width="180" />
      <el-table-column label="收款人姓名" align="center" prop="name" width="150" />
      <el-table-column label="转账卡类型" align="center" prop="transferCardType" width="90">
        <template slot-scope="{ row }">
          <span v-if="row.transferCardType === 1">个人</span>
          <span v-if="row.transferCardType === 2">对公</span>
        </template>
      </el-table-column>
      <el-table-column label="银行卡号" align="center" prop="bankCardNum" width="180" />
      <el-table-column label="银行名称" align="center" prop="bankName" width="110" />
      <el-table-column label="转账金额" align="center" prop="amount" width="120" />
      <el-table-column label="状态" align="center" prop="status" width="90">
        <template slot-scope="{ row }">
          <span v-if="row.status === 1">转账中</span>
          <span v-if="row.status === 2">转账成功</span>
          <span v-if="row.status === 3">转账失败</span>
          <span v-if="row.status === 4">转账取消</span>
        </template>
      </el-table-column>
      <el-table-column label="转账时间" align="center" prop="createTime" width="180" />
      <el-table-column label="到账时间" align="center" prop="paymentDate" width="180"></el-table-column>
      <el-table-column label="转账备注" align="center" prop="remark" />
      <el-table-column label="电子回单" align="center">
        <template slot-scope="{row}">
          <template v-if="row.status === 2"><!--转账成功-->
            <template v-if="row.receiptStatus === 1">申请中</template>
            <template v-else-if="row.receiptStatus === 2">
              <el-link type="success" icon="Download" :href="row.receiptUrl" download>下载</el-link>
            </template>
            <template v-else-if="row.receiptStatus === 3">
              申请失败 {{ row.receiptUrl }}
            </template>
            <template v-else-if="['ecs_yee_pay', 'ecs_baofu_merchant'].indexOf(row.tableName) >= 0">
              <el-link type="primary" icon="DocumentAdd" @click="handleReceipt(row)" v-clickOnce>申请</el-link>
            </template>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

  </div>
</template>

<script>
import { listExternalTransferRecord, getExternalTransferReceipt } from "@/api/ecs/externalTransferRecord";

export default {
  name: "ExternalTransferRecord",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      dateRange: [],
      //时间快捷范围
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      // 对外转账记录表格数据
      externalTransferRecordList: [],
      // 弹出层标题
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shopIncomingId: null,
        requestId: null,
        platformId: null,
        agentId: null,
        childAgentId: null,
        shopId: null,
        userId: null,
        serialNumber: null,
        name: null,
        transferCardType: null,
        bankCardNum: null,
        bankName: null,
        amount: null,
        shopName: null,
        subMerchantId: null,
        tableName: null,
        status: null,
        paymentDate: null,
        fail: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询对外转账记录列表 */
    getList() {
      this.loading = true;
      listExternalTransferRecord(this.addDateRange(this.queryParams, this.dateRange, "PaymentDate")).then(response => {
        this.externalTransferRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ecs/externalTransferRecord/export', {
        ...this.queryParams
      }, `externalTransferRecord_${new Date().getTime()}.xlsx`)
    },
    // 申请回单
    handleReceipt(row) {
      getExternalTransferReceipt(row.id).then(() => {
        this.getList()
      })
    },
  }
};
</script>
